import { RootAction, RootState, RootThunkAction } from '@/types/store'
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

export const useInitialState = <T extends keyof RootState>(
  action: () => RootThunkAction | RootAction,
  stateName: T
) => {
  const dispatch = useDispatch()
  useEffect(() => {
    dispatch(action())
  }, [dispatch, action])

  return useSelector((state: RootState) => state[stateName])
}
